//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Main.h"
#include "ChangePass.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TChangePassword *ChangePassword;

//---------------------------------------------------------------------------
bool __fastcall TChangePassword::IsValidChange(void)
{
	bool rc = true;
	if ( "" == OldPassEd->Text ) rc = false;
	if ( "" == NewPassEd->Text ) rc = false;
	if ( false == AdjChangeRb->Checked && false == SettingChangeRb->Checked ) rc = false;
	return (rc);
}

//---------------------------------------------------------------------------
__fastcall TChangePassword::TChangePassword(TComponent* Owner)
	: TForm(Owner)
{
}

//---------------------------------------------------------------------------
void __fastcall TChangePassword::FormShow(TObject *Sender)
{
	OldPassEd->Clear();
	NewPassEd->Clear();
	AdjChangeRb->Checked = false;
	SettingChangeRb->Checked = false;
	OKBt->Enabled = false;
}
//---------------------------------------------------------------------------
void __fastcall TChangePassword::CancelBtClick(TObject *Sender)
{
	ShowMessage("Password isn't changed!");
	ChangePassword->Close();
}
//---------------------------------------------------------------------------
void __fastcall TChangePassword::AdjChangeRbClick(TObject *Sender)
{
	OKBt->Enabled = IsValidChange();
}
//---------------------------------------------------------------------------
void __fastcall TChangePassword::SettingChangeRbClick(TObject *Sender)
{
	OKBt->Enabled = IsValidChange();
}
//---------------------------------------------------------------------------
void __fastcall TChangePassword::NewPassEdChange(TObject *Sender)
{
	OKBt->Enabled = IsValidChange();
}
//---------------------------------------------------------------------------
void __fastcall TChangePassword::OldPassEdChange(TObject *Sender)
{
	OKBt->Enabled = IsValidChange();
}
//---------------------------------------------------------------------------
void __fastcall TChangePassword::OKBtClick(TObject *Sender)
{
	MotorControl->pPassADO->First();

	if ( true == AdjChangeRb->Checked )
	{
		String dbPass = MotorControl->pPassADO->FieldValues["Adj"];
		if ( dbPass == OldPassEd->Text )
		{
			MotorControl->pPassADO->Edit();
			MotorControl->pPassADO->FieldValues["Adj"] = NewPassEd->Text;
			MotorControl->pPassADO->Post();
		}
		else
		{
			ShowMessage("Current password incorrect!");
			return;
        }
	}

	if ( true == SettingChangeRb->Checked )
	{
		String dbPass = MotorControl->pPassADO->FieldValues["Setting"];
		if ( dbPass == OldPassEd->Text )
		{
			MotorControl->pPassADO->Edit();
			MotorControl->pPassADO->FieldValues["Setting"] = NewPassEd->Text;
			MotorControl->pPassADO->Post();
		}
		else
		{
			ShowMessage("Current password incorrect!");
			return;
        }
	}

	ChangePassword->Close();
}
//---------------------------------------------------------------------------
void __fastcall TChangePassword::FormClose(TObject *Sender, TCloseAction &Action)

{
	MotorControl->Enabled = true;
}
//---------------------------------------------------------------------------
